import { ColumnType } from 'antd/es/table';
import { ModalProps, TableProps } from '@inbiz/antd';
import React, { Dispatch, ReducerAction, RefObject } from 'react';
import { ButtonType } from 'antd/lib/button/button';
import { inbiz as IPageInbiz } from '@inbiz/render';
import { DrawerProps } from 'antd/lib/drawer';
import { ActionEnum, ConnectorEnum } from './shared';
import {
  IModalCallBackEvents,
  IModalEmit,
} from '@inbiz/render';
import { OrderInfo } from '@inbiz/hooks';
import { UploadFile } from 'antd/lib/upload/interface';

export type SearchType = { searchable: boolean; scrollY?: number };

export interface ActionConfigType {
  id: number | string;
  actionName: string;
  actionType: ActionEnum;
  buttonColor?:string;
  isDropAction?:boolean;
  openMode?: string;
  paramField?: string;
  page?: string;
  position?: string;
  icon?: { type: string; color: string };
  type?: string;
  windowName?: string;
  parentAction?: string;
  parentType?: string;
  render?: (className?: string) => React.ReactNode;
}

export interface ActionsProps {
  data?: any;
  className?: string;
  style?: React.CSSProperties;
  actions?: ActionConfigType[];
  leftAuthActions?: ActionConfigType[];
  authActions?: ActionConfigType[];
  displayQty?: number;
  pageRef?: React.RefObject<{
    page: IPageInbiz;
  }>;
  actionDisplaySetup?: string;
  isTableActions?: boolean;
  index?: number;
  tableHeaderBtnShow?: string;
  inLineBtnShow?: string;
  buttonColor?:string;
  isDropAction?:boolean;
  topBtnMorePopover: boolean, 
  buttonPosition: 'left' | 'right'
}

export interface ActionProps extends ActionConfigType {
  data?: any;
  buttonType?: ButtonType;
  buttonColor?:string;
  isDropAction?:boolean;
  className?: string;
  actionDisplaySetup?: string;
  loading?: boolean;
  operationflag?: string;
  disabled?: boolean;
  updateLoading?: () => boolean;
  dispatch?: (state?: any, action?: any) => any;
}

export interface PropConfigType {
  id: string;
  columnName: string;
  propName: string;
  title?: string;
  label?: string;
  placeKey?: string;
  placeText?: string;
  typeValue?: 'obj' | 'array';
  children?: ItemChildren[];
  order?: number;
  width?: number;
  sorter?: string;
  fixed?: 'left' | 'right' | 'none';
  align?: AlignType;
}

export type ModalConfig = {
  actions: ActionConfigType[];
  width: number;
  maxHeight: number;
  minHeight: number;
};

export type ToolbarType = {
  actions?: ActionProps[];
  displayQty: number;
  actionDisplaySetup?: string;
};

export type QueryParam = {
  current: number;
  pageSize: number;
  orders?: OrderInfo[];
};

export type QueryParams = {
  pageInfo?: PageInfo;
  orders?: OrderInfo[];
  conditions?: ConditionsType[];
  iConditions?: { [key: string]: any };
  sort?: boolean;
  currentId?: string;
  reference?: string;
  position?: 'before' | 'after';
  sortField?: string;
};

export interface TableModalProps extends ModalProps, InBizProps {
  data?: any;
  state?: any;
  openMode?: string;
  actionType?: string;
  actions?: ActionConfigType[];
  height?: number;
  widthSuffix?: '%' | 'px';
  heightSuffix?: '%' | 'px';
  onClose?: (param?: any) => void;
  dataSourceRef?: RefObject<RecordType[]>;
  primaryKey: string;
}

export interface TableDrawerProps extends Omit<DrawerProps, 'onClose'>, InBizProps {
  actionType?: string;
  openMode?: string;
  actions?: ActionProps[];
  widthSuffix?: '%' | 'px';
  heightSuffix?: '%' | 'px';
  dispatch?: (action: {}) => void;
  onClose?: (param?: any) => void;
  render?: React.ReactNode;
  pageRef?: React.RefObject<{
    page: IPageInbiz;
  }>
}

export type SubWindowType = {
  width?: { widowWidth?: number; widthSuffix?: string };
  height: { windowHeight?: number; heightSuffix?: string };
  maxHeight: { height: number; suffix: string };
  minHeight: { height: number; suffix: string };
  actionDisplaySetup?: string;
  tableHeaderBtnShow?: string;
  inLineBtnShow?: string;
};

export type ExportType = {
  fields?: any[];
  conditions?: any[];
  appId?: string;
  sourceModel?: string;
  fileName?: string;
  recordIds?: string;
  importUpdate?: boolean;
  fileType?: number;
  pageInfo?: {
    index: number;
    size: number;
  };
  orderInfo?: {};
};

export type ImportType = {
  file: UploadFile;
  fields?: any[];
  appId?: string;
  sourceModel?: string;
  importUpdate?: boolean;
};

export type DownloadTmplType = {
  fields?: any[];
  appId?: string;
  sourceModel?: string;
  fileName?: string;
  importUpdate?: boolean;
};

export interface SubWindowConfigType {
  id: string;
  parentAction?: string;
}

export interface RecordType {
  [key: string]: any;
}
export interface CardTmplValueProps {
  name: string;
  id: string;
  isError?: boolean;
}
export type VisibleColumnType = { visible?: string } & ColumnType<RecordType>;

export type TitleConfig = { status?: boolean; value?: string };

export type ItemChildren = {
  placeKey: string;
  placeText: string;
  label: string;
  propName: string;
  order: number;
  id?: string;
};

export type ItemData = {
  typeValue: 'obj' | 'array';
  placeKey: string;
  placeText: string;
  label: string;
  propName: string;
  order: number;
  id?: string;
  children?: ItemChildren[];
};

export type ColumnConfig = {
  tableValues?: {
    actions?: ActionConfigType[];
    attrs?: PropConfigType[];
    subWindow?: ActionConfigType[];
  };
  formItems?: {
    [key: string]: any;
  };
  queryFields?: { value: string | number; label: string }[];
};

export type AlignType = 'left' | 'center' | 'right';

export type ButtonSize = 'middle' | 'large';

export type Expanded = 'expanded' | 'collapse';

export type ExpandedButton = 'hidden' | 'visible';

export type ExpandedSize = 'all' | 'oneLine';

export interface SearchProps {
  fetchData: () => void;
}

export type PageData = {
  data?: RecordType[];
  total?: number;
  primary?: string;
  relationField?: string;
};

export interface TableTopProps {
  children?: React.ReactNode;
}

export type ScrollType = { x?: number | true | string; y?: number | string };

export type ConditionsType = {
  connector: ConnectorEnum;
  condition: any[];
  index: number;
};

type ISubPageEmit = {
  (type: 'onSubPageInit', inbiz: inbiz): void;
  (type: 'onSubPageMount', inbiz: inbiz): void;
};

export interface InBizProps {
  sourceModel?: string;
  appInfo?: any;
  sourcetype?: 'model' | string;
  modelkey?: string;
  inbiz?: IPageInbiz;
  page?: string;
  callBackEvents?: { onBeforeChange?: (v: string) => boolean } & IModalCallBackEvents;
  emit?: IModalEmit & ISubPageEmit;
  pageRef?: RefObject<{ page: IPageInbiz }>;
}

export type QueryType = { currentPage?: number; pageSize?: number; params?: any; orders?: any };
export type PageInfo = { current: number; pageSize: number };

export type TableCtxType = InBizProps & {
  attrs?: PropConfigType[];
  isDesignable?: boolean;
  state?: any;
  sourceModel: string;
  modelKey: string;
  dispatch?: Dispatch<ReducerAction<any>>;
  importUpdate?: boolean;
  modalButtons?: {
    actions?: ActionConfigType[];
    width: number;
    height: number;
  };
  dataSourceRef?: RefObject<RecordType[]>;
  refresh: () => any;
  handlePrint: () => any;
  fetchTagData?: () => void;
  tableRef: RefObject<any>;
  tableCacheRef: RefObject<any>;
  eventsRef?: RefObject<any>;
  emit?: Function;
  selectedRowKeys?: React.Key[];
  conditions?: { [key: string]: any };
  cardsMap?: Map<string, any>;
};

export interface TablePreviewProps extends Omit<TableProps<RecordType>, 'title'>, InBizProps {
  boxHeight: string;
  autoLoad?: boolean;
  showLineNumber?: boolean; //显示行号
  controlId: string;
  modelkey?: string;
  modelname?: string;
  isDesignable?: boolean; //是否设计器模式
  resizeColumnWidth?: boolean; //列宽调整,
  showPage?: boolean; //是否显示分页
  pageRefresh?: boolean; //分页刷新
  align?: AlignType; //列宽调整,
  mergeRow?: boolean; //合并行,
  search?: boolean;
  searchExpandedSize?: ExpandedSize;
  expandedButton?: ExpandedButton;
  searchButtonPosition?: AlignType;
  buttonRegion?: boolean; // 按钮区域
  buttonPosition?: 'left' | 'right'; // 按钮位置
  searchButtonSize?: ButtonSize;
  searchExpanded?: Expanded;
  title?: (data: readonly RecordType[]) => React.ReactNode | TitleConfig;
  importUpdate?: boolean;
  zebraEffect?: boolean;
  columnConfig?: ColumnConfig;
  thumbnail?: boolean; // 缩略图
  thumbConfig?: ColumnConfig; // 缩略图配置
  showPageType?: 'list' | 'thumbnail'; // 当视图为列表视图时，显示的默认视图
  height?: number;
  tableViewType?: 'table' | 'card';
  inbiz?: inbiz;
  searchPlacehoder?: string;
  rowHeight: any;
  selectComponent?: string;
  relationField?: string;
  columnResetName?: string;
  dragSort?: boolean;
  sortField?: string;
  sortInterface?: { [key: string]: any };
  showSortIcon?: boolean;
  showPosition?: 'left' | 'right';
  zebraEffectColor?: string; // 斑马线背景
  tableHeaderColor?: string; // 表头背景
  primarykey: string; // 表格数据的唯一key
  showHeightRule?: string; // 高度显示规则
  boxPadding?: number; // 表格容器的paddingTop和paddingBottom
  boxMargin?: number; // 表格容器的marginTop和marginBottom
  saveSearch?: boolean; // 保存查询条件
  clickSearch?: boolean; // 点击默认查询
  pressDelay?: number;//设置表格拖动的延迟时间
  previewMode: boolean;
  changeView?: () => void;
  viewMode?:string;
  pagination?: object
  virtual?: { //大数据量下， 延迟渲染渲染配置
    loadingText?: string,
    defaultNumber?: number // 默认渲染条数，渲染数量大于defaultNumber 才执行延迟渲染
    virtualRun?: (show:Function, index: number) => Function //延迟渲染组件的 显示逻辑， 返回的Function用于组件销毁时执行，类似于useEffect
  }
}

export type FormatColumnsType = {
  eventsRef?: RefObject<any>;
};

export type ManualRelations = {
  LeftModelKey: string;
  LeftModelName: string;
  LeftFieldKey: string;
  LeftFieldName: string;
  RightModelKey: string;
  RightModelName?: string;
  RightFieldKey: string;
  RightFieldName?: string;
  JoinType?: string;
  Order?: number;
  id?: string;
};

export type ImodelDetail = {
  Id: String;
  Name: String;
  appId: String;
  ModelQueryConfig: {
    FilterType?: number;
    ModelQueryModelRelation: {
      AnotherName?: string;
      ModelKey: string;
      ModelName: string;
      ParentModelKey?: string;
    }[];
    ModelQueryField: {
      ModelName: string;
      ModelKey: string;
      FieldName: string;
      FieldKey: string;
      AnotherName?: string;
      RelationDictionary?: string;
      FormatFactory: object;
    }[];
    SortConfigs?: {
      ModelName: string;
      ModelKey: string;
      FieldName: string;
      FieldKey: string;
      Order: number;
      SortType: 'desc' | 'asc';
    }[];
    UseCustomRelation?: boolean;
    CustomModelRelation?: ManualRelations[];
    FixedCondition?: {
      ConditionStr: string;
    };
  };
};
